package com.example.teaching_server.controller;

import com.example.teaching_server.common.ApiResponse;
import com.example.teaching_server.dto.ActivityBaseDTO;
import com.example.teaching_server.entity.ActivityBaseEntity;
import com.example.teaching_server.repository.ActivityBaseRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/api/activity-bases")
@RequiredArgsConstructor
@Tag(name = "活动基地管理", description = "活动基地相关接口")
public class ActivityBaseController {
    private final ActivityBaseRepository activityBaseRepository;

    /**
     * 获取所有基地列表
     */
    @GetMapping
    @Operation(summary = "获取基地列表", description = "获取所有活动基地的详细信息")
    public ApiResponse<List<ActivityBaseDTO>> getAllBases() {
        log.info("开始获取活动基地列表");

        try {
            List<ActivityBaseEntity> bases = activityBaseRepository.findAll();
            List<ActivityBaseDTO> baseDTOs = bases.stream()
                    .map(this::convertToDTO)
                    .collect(Collectors.toList());

            log.info("成功获取{}个活动基地", baseDTOs.size());
            return ApiResponse.success("基地列表获取成功", baseDTOs);
        } catch (Exception e) {
            log.error("获取基地列表失败", e);
            return ApiResponse.fail("获取基地列表失败: " + e.getMessage());
        }
    }

    /**
     * 将实体转换为DTO
     */
    private ActivityBaseDTO convertToDTO(ActivityBaseEntity entity) {
        ActivityBaseDTO dto = new ActivityBaseDTO();
        dto.setId(entity.getId());
        dto.setName(entity.getName());
        dto.setDescription(entity.getDescription());
        dto.setAddress(entity.getAddress());
        return dto;
    }
}
